<?php

// jTranslate.php
// A simple server-side proxy for the Google Translate API v2
// Designed to accept a post request from an xmlhttprequest object, shuttle args to and from Google, and return the response.

$debug = 0; // set to 1 to enter debug mode.

$serviceUrl = 'https://www.googleapis.com/language/translate/v2'; // url for the translate api
if(isset($_POST['jgAction'])){
 $serviceUrl .= '/' . $_POST['jgAction'];
}

$args = array();
foreach($_POST as $key => $val){
 if($key == 'jgAction'){
  continue;
 }
 if(is_array($val)){
  foreach($val as $v){
   $args[] = preg_replace("/\[\]/", '', $key) . '=' . urlencode($v);
  }
 }else{
  $args[] = preg_replace("/\[\]/", '', $key) . '=' . urlencode($val);
 }
}
$queryString = implode('&', $args);
//$queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : implode('&',$argv[1]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serviceUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-HTTP-Method-Override: GET'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $queryString);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

if($debug){ // some debugging stuff
 curl_setopt($ch, CURLOPT_HEADER, 1);
 print "// ".sizeOf($args)."\n";
 print "// {$serviceUrl}\n";
 print "// {$queryString}\n\n";
}

$body = curl_exec($ch);
print $body;
curl_close($ch);
exit(0);

?>